﻿CREATE PROCEDURE [dbo].[DefekturaGoodsUpdate]
(
	@DefekturaCategoryId int,
	@Original_GoodsId int
)
AS
DECLARE @strMsg nvarchar(500), @intRet int, @intErrCode int
DECLARE @orgDefekturaCategoryId int

SET NOCOUNT ON

IF (@Original_GoodsId Is Null) OR (@DefekturaCategoryId Is Null)
BEGIN
	SELECT @intErrCode = 1, @strMsg = 'Неправильные параметры в вызове процедуры ' + Object_Name(@@Procid) + ' !'
	RAISERROR (@strMsg, 11, @intErrCode)
	RETURN @intErrCode
END

SELECT @orgDefekturaCategoryId = DefekturaCategoryId
FROM PR.DefekturaGoods WHERE GoodsId = @Original_GoodsId
IF (@@rowcount <> 1)
BEGIN
	SELECT @intErrCode = 2, 
	@strMsg = 'Данные изменены после последнего запроса!'
	RAISERROR (@strMsg, 11, 1)
	RETURN @intErrCode
END

	SET NOCOUNT OFF;

UPDATE PR.DefekturaGoods SET 
DefekturaCategoryId = @DefekturaCategoryId
WHERE GoodsId = @Original_GoodsId;
	
SELECT GoodsId, DefekturaCategoryId
FROM PR.DefekturaGoods WHERE GoodsId = @Original_GoodsId

RETURN 0
